﻿package com.tudou.home.model
{
    import com.tudou.home.commands.*;
    import com.tudou.home.model.data.*;
    import com.tudou.js.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class PlayListProxy extends Proxy implements IProxy
    {
        private var playIndex:uint = 0;
        private var jsExchanger:JSExchanger;
        private var playList:Array;
        private var pageInfo:PageInfo;
        public static const NAME:String = "PlayListProxy";

        public function PlayListProxy()
        {
            super(NAME);
            pageInfo = PageInfo.getInstance();
            jsExchanger = JSExchanger.getInst();
            return;
        }// end function

        public function item(param1:String, param2:Boolean = false) : void
        {
            if (playList == null)
            {
                return;
            }
            var _loc_3:uint = 0;
            while (_loc_3 < playList.length)
            {
                
                if (playList[_loc_3].iid == param1)
                {
                    break;
                }
                _loc_3 = _loc_3 + 1;
            }
            thisIndex(_loc_3);
            return;
        }// end function

        private function addJavaSriptHlr() : void
        {
            jsExchanger.shareData(this, "playlist");
            jsExchanger.addCallback(this, "nextItem");
            jsExchanger.addCallback(this, "prevItem");
            jsExchanger.addCallback(this, "item");
            return;
        }// end function

        public function getPlayList() : Array
        {
            return playList;
        }// end function

        public function getIndex() : uint
        {
            return playIndex;
        }// end function

        public function set playlist(param1:Array) : void
        {
            var _loc_3:Object = null;
            var _loc_4:ListItem = null;
            var _loc_5:int = 0;
            var _loc_2:* = param1;
            if (_loc_2 && _loc_2.length > 0)
            {
                _loc_5 = 0;
                while (_loc_5 < _loc_2.length)
                {
                    
                    _loc_3 = _loc_2[_loc_5];
                    _loc_4 = playList[_loc_5];
                    _loc_4.expandStage = _loc_3.scale == "1";
                    _loc_5++;
                }
            }
            return;
        }// end function

        public function nextItem() : void
        {
            thisIndex((playIndex + 1));
            return;
        }// end function

        private function judgeResult(param1:Object) : Boolean
        {
            var list:Array;
            var result:* = param1;
            try
            {
                list = result as Array;
                if (list == null || list.length <= 0)
                {
                    return false;
                }
            }
            catch (e:Error)
            {
                return false;
            }
            return true;
        }// end function

        public function prevItem() : void
        {
            thisIndex((playIndex - 1));
            return;
        }// end function

        public function loadPlayList() : void
        {
            jsExchanger.getData("playlist", fromPageLoaded, judgeResult);
            addJavaSriptHlr();
            return;
        }// end function

        public function thisIndex(param1:uint) : void
        {
            if (playList == null)
            {
                return;
            }
            if (param1 >= playList.length)
            {
                param1 = 0;
            }
            playIndex = param1;
            var _loc_2:* = playList[playIndex] as ListItem;
            pageInfo.iid = _loc_2.iid;
            pageInfo.needCount = _loc_2.needCount;
            pageInfo.channelId = _loc_2.channelId;
            pageInfo.comScoreC6 = _loc_2.comSocreC6;
            pageInfo.nielsenStat = _loc_2.nielsenStat;
            pageInfo.maxRateType = _loc_2.maxRateType;
            pageInfo.original = _loc_2.original;
            pageInfo.originalWidth = _loc_2.originalWidth;
            pageInfo.originalHeight = _loc_2.originalHeight;
            pageInfo.originalRate = _loc_2.originalRate;
            pageInfo.skipHeadValue = _loc_2.skipHeadValue;
            pageInfo.skipTailValue = _loc_2.skipTailValue;
            pageInfo.isPrivate = _loc_2.isPrivate;
            pageInfo.isToll = _loc_2.isToll;
            pageInfo.title = _loc_2.title;
            pageInfo.expandStage = _loc_2.expandStage;
            pageInfo.isUserOwner = _loc_2.isUserOwner;
            sendNotification(PlayListCommandSet.CHANGE_ITEM, [playIndex, playList.length]);
            return;
        }// end function

        private function fromPageLoaded(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:ListItem = null;
            var _loc_5:int = 0;
            var _loc_2:* = param1 as Array;
            playList = [];
            if (_loc_2.length <= 0)
            {
                return;
            }
            _loc_5 = 0;
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_5];
                _loc_4 = new ListItem();
                _loc_4.iid = _loc_3.iid;
                _loc_4.channelId = _loc_3.cid;
                _loc_4.needCount = _loc_3.nc == "1";
                _loc_4.comSocreC6 = _loc_3.csc6;
                _loc_4.nielsenStat = _loc_3.nls;
                _loc_4.title = _loc_3.title;
                _loc_4.maxRateType = _loc_3.hd;
                _loc_4.original = _loc_3.ol == "1";
                _loc_4.originalWidth = _loc_3.olw;
                _loc_4.originalHeight = _loc_3.olh;
                _loc_4.originalRate = _loc_3.olr;
                _loc_4.skipHeadValue = _loc_3.sh;
                _loc_4.skipTailValue = _loc_3.st;
                _loc_4.isToll = _loc_3.nm;
                _loc_4.isPrivate = _loc_3.np == "1";
                _loc_4.expandStage = _loc_3.scale == "1";
                _loc_4.isUserOwner = _loc_3.ban == "1";
                playList[_loc_5] = _loc_4;
                if (_loc_4.iid == pageInfo.iid)
                {
                    playIndex = _loc_5;
                }
                _loc_5++;
            }
            sendNotification(PlayListCommandSet.PLAY_LIST_LOADED, [playList, playIndex]);
            return;
        }// end function

    }
}
